<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Customer extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		if(!$this->session->userdata('user_id')){
			redirect('/',301);
		}
	}
	
	public function index(){
		redirect('customer/active/');
	}	
	
	public function active($offset=0)
	{
		$data['title']='Customer';
		$paging=5;
		$this->session->set_userdata('customer','customer/active/'.$offset);
		if($this->input->post('search')){
			$keyword = $this->input->post('search');
		} else {
			$keyword = '';
		}
		
		$data['activity']=$this->customers->getAllCustomer($keyword,$this->session->userdata('user_id'),'id','DESC',$offset,$paging);
		$num_records = COUNT($this->customers->getAllCustomer($keyword,$this->session->userdata('user_id')));  
        if ($num_records > $paging) {
        	    $this->load->library('pagination');
	            $config['base_url']   = site_url('customer/active/');
	            $config['total_rows'] = $num_records;
	            $config['per_page']   = $paging;
	            $config['uri_segment']= 3;
	            $this->pagination->initialize($config);	 
	            $data['pagination']   = $this->pagination->create_links();
        }
		$data['content'] = 'customer-list';
		$this->load->view('index',$data);
	}
	
	public function history($id)
	{
		$data['title']='History';
		$paging=100;
		$offset=0;
		$this->db->where('id',$id);
		$Q=$this->db->get('customers');
		$data=$Q->row_array();
		$this->session->set_userdata('customer_name',$data['customer_name']);
		$data['customer']	= $data['customer_name'];	
		$data['activity']=$this->activities->getAllActivity($id,$this->session->userdata('user_id'),'id','DESC',$offset,$paging);
		$data['content'] = 'customer-history';
		$this->load->view('index',$data);
	}
	
	
	public function add(){
		$data['title']='Add Customer';
		$data['group'] =$this->groups->getActive();
		$data['sector'] =$this->sectors->getActive();
		$data['source'] =$this->sources->getActive();
		$data['status'] =$this->status->getActive($this->config->item('prospect'));
		$data['action'] = 'customer/save';
		$data['content'] = 'customer-form';
		$this->load->view('index',$data);
		
	}
	
	function save(){
		if($this->input->post('save')):
              $this->customers->getSave();
              $this->session->set_flashdata('success','success');
              redirect($this->input->server('HTTP_REFERER'));
        else:
              $this->session->set_flashdata('error','error');
              redirect($this->input->server('HTTP_REFERER'));
        endif;	
	}
	
	public function remove($id)
    {
		if($this->customers->getCountCustomer($id,$this->session->userdata('user_id'))<1){
			$this->session->set_flashdata('error_message','error');
			if($this->session->userdata('customer'))
				redirect($this->session->userdata('customer'),301);
			else
				redirect('customer/active',301);
		}
		
        $this->customers->remove($id);
		$this->session->set_flashdata('delete_message','success');
		if($this->session->userdata('customer'))
				redirect($this->session->userdata('customer'),301);
		else
				redirect('customer/active',301);
    }
	
	public function approval($offset=0)
	{
		$data['title']='Customer';
		$paging=7;
		$this->session->set_userdata('customer','customer/active/'.$offset);
		$data['prospect']=$this->customers->getApproval($paging,$this->config->item('prospect'));
		$data['existing']=$this->customers->getApproval($paging,$this->config->item('existing'));
		$data['content'] = 'approval-list';
		$this->load->view('index',$data);
	}
	
	function updateProspect($id){
		if($id):
				if($this->session->userdata('user_pos')==$this->config->item('se')):
					$this->customers->getUpdateProspectTA($id);
				elseif ($this->session->userdata('user_pos')==$this->config->item('spv')):
					$this->customers->getUpdateProspectSE($id);
				endif;		
                $this->session->set_flashdata('success','<span class="message success"><strong>Success</strong>message - '.$this->lang->line('m_update').'  </span>');
                redirect($this->input->server('HTTP_REFERER')); 
        else:
			$this->session->set_flashdata('success','<span class="message error"><strong>Error</strong>message - no Choose Customer </span>');
                redirect($this->input->server('HTTP_REFERER')); 
		endif;
	}
	
	function updateReject($id){
		if($id):
				if($this->session->userdata('user_pos')==$this->config->item('se')):
					$this->customers->getDeleteCustomer($id);
				elseif ($this->session->userdata('user_pos')==$this->config->item('spv')):
					$this->customers->getProspectUser($id);
				endif;		
                $this->session->set_flashdata('error','error');
                redirect($this->input->server('HTTP_REFERER')); 
        else:
			$this->session->set_flashdata('success','<span class="message error"><strong>Error</strong>message - no Choose Customer </span>');
                redirect($this->input->server('HTTP_REFERER')); 
		endif;
	}
	
	
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */